<?php

/*
Copyright 2011 Saiyasodharan (http://saiy2k.blogspot.com/)

This file is part of SpiroCanvas (http://code.google.com/p/spirocanvas/)

SpiroCanvas is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SpiroCanvas is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SpiroCanvas.  If not, see <http://www.gnu.org/licenses/>.
*/

require 'fbsdk/facebook.php';				//including facebook SDK
require 'config.php';

//initialize facebook object
$facebook = new Facebook(array(
                           'appId'  => $FACEBOOK_APPID,
                           'secret' => $FACEBOOK_SECRET,
                          'fileUpload' => true, 
                           'cookie' => true,));
$facebook->setFileUploadSupport(true);  

//get the base64 data from POST arguments
$b64	=	$_POST['data'];

//remove this "data:image/png;base64" header from the base64 data
$clean	=	substr($b64, 22);

//replace blanks(generated by toDataURL()) with pluses for base64_decode to work correct
$clean = str_replace(' ','+',$clean);

//decode the raw data and save it in a file
$FILE_PATH = $_POST['uid'] . '.png';
file_put_contents($FILE_PATH, base64_decode($clean));

//post the saved file to FB
$args	=	array('message' => '');
$args['image']	=	'@' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);

//to delete the file
unlink($FILE_PATH);

echo 'success';

?>